<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        img {
            opacity: 0;
            transition: all 2s;
            width: 500px;
            position: absolute;
            left: 0;
            top: 0;

        }

        .main {
            position: relative;
        }
    </style>

</head>

<body>
    <div class="main">
        <img src="./img/bg1.jpg" alt="">
        <img src="./img/bg2.jpg" alt="">
        <img src="./img/bg3.jpg" alt="">


    </div>

    <script>

        var imgs = document.getElementsByTagName("img");
        var curIndex = 0;   //数组的索引
        var imgslength = imgs.length; //variable

        var isVisible = true; //false

        var isAsc = true;

        var timer = setInterval(function () {

            // alert("hello");
            if (isVisible) {
                imgs[curIndex].style.opacity = "1";
            }
            else {
                imgs[curIndex].style.opacity = "0";
            }

            // imgs[curIndex].style.display =isVisible?"block":"none";



            if (isAsc && curIndex == imgslength - 1) {

                isAsc = false;
                //curIndex = 0;
                //curIndex--;
                curIndex++;

                isVisible = !isVisible;
            }
            else if (isAsc == false && curIndex == 0) {
                isAsc = true;
                curIndex--;
                isVisible = !isVisible;
            }

            if (isAsc) {
                curIndex++;
            }
            else {
                curIndex--;
            }

        }, 2000);


    </script>
</body>

</html>